﻿using MiniFox.Infrastructure;
using MiniFox.Utility;
using MiniFox.Web;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace WebApi.Test
{
    internal class DeviceServiceTester
    {
        static string currentId;
        public static void Create()
        {
            var uri = new Uri( $"{UriExtensions.DS_URI}/device/create");
            var requestObj = new DeviceDto
            {
                deviceId = string.Empty,
                snCode = Tools.GenerateRandomCode(20),
                displayCode = Tools.GenerateRandomCode(10),
                deviceName = Tools.GenerateRandomCode(20),
                deviceType = Tools.GenerateRandomCode(5)
            };
            var result = uri.HttpPostJsonAsync<DeviceDto, Response<DeviceVo>>(requestObj).Result;

            currentId = result.data.deviceId;
        }

        public static void Update()
        {
            var uri = new Uri( $"{UriExtensions.DS_URI}/device/update");
            var requestObj = new DeviceDto
            {
                deviceId = currentId,
                snCode = Tools.GenerateRandomCode(20),
                displayCode = Tools.GenerateRandomCode(10),
                deviceName = Tools.GenerateRandomCode(20),
                deviceType = Tools.GenerateRandomCode(5)
            };
            var result = uri.HttpPostJsonAsync<DeviceDto, Response<DeviceVo>>(requestObj).Result;
        }
        public static void Toggle()
        {
            var uri =new Uri($"{UriExtensions.DS_URI}/device/toggle");
            
            var responseObj = uri.HttpPostJsonAsync<string, Response<DeviceVo>>(currentId).Result;
        }
        public static void Delete()
        {
            var uri = new Uri( $"{UriExtensions.DS_URI}/device/delete");

            var responseObj = uri.HttpPostJsonAsync<string, Response<string>>(currentId).Result;
        }

        public static void GetById()
        {
            Console.WriteLine("请输入要查询的设备id");
            var uri = new Uri($"{UriExtensions.DS_URI}/device/get");
            //uri = uri.AddQueryString(new { deviceId = Console.ReadLine() });
            var responseObj = uri.HttpGetAsync<Response<DeviceVo>>(new { deviceId = Console.ReadLine() }).Result;
            Console.WriteLine("响应体：" + responseObj.ToJsonString());
        }

        public static void Query()
        {
            var uri = new Uri($"{UriExtensions.DS_URI}/device/query");
            Console.WriteLine("请输入要查询关键字(匹配设备名)");

            string keyword = Console.ReadLine();
            var requestObj = new PagedQueryObject
            {
                SortField = "",
                SortDesc = false,
                PageIndex = 2,
                PageSize = 5,
                Parameters = new
                {
                    Keyword = keyword
                }
            };
            Console.WriteLine("请求体：" + requestObj.ToJsonString());
            var json = uri.HttpPostJsonAsync<PagedQueryObject, Response<PagedResultObject<DeviceVo[]>>>(requestObj).Result;
            Console.WriteLine("响应体：" + json.ToJsonString());
        }
    }

    public class DeviceDto
    {
        public string deviceId;
        public string snCode;
        public string displayCode;
        public string deviceName;
        public string deviceType;
    }

    public class DeviceVo
    {
        public string deviceId;
        public string snCode;
        public string displayCode;
        public string deviceName;
        public string deviceType;
        public string createTime;
        public string updateTime;
    }


    public class Response<T>
    {
        public T data;
        public bool success;
        public string message;
        public string code;
    }
}
